package controllers

import (
	"babydoge/common/random"
	"babydoge/common/requests"
	"babydoge/common/responses"
	"babydoge/enum/code"
	"babydoge/enum/fileds"
	"babydoge/models"
	"babydoge/processor"
	"babydoge/repositories"
	"github.com/labstack/echo/v4"
	"net/http"
	"time"
)

type QueueController struct {
	repository *repositories.QueueRepository
}

func NewQueueController() (queueController *QueueController, err error) {
	var (
		queueRepository *repositories.QueueRepository
	)

	if queueRepository, err = repositories.NewQueueRepository(); err != nil {
		return &QueueController{}, err
	}
	return &QueueController{
		repository: queueRepository,
	}, err
}

func (controller *QueueController) SaveQueue(c echo.Context) error {
	var (
		v      *requests.QueueRequest
		id     string
		s      *models.Queue
		second int
		err    error
	)

	v = new(requests.QueueRequest)
	if err = c.Bind(v); err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "绑定参数失败", err))
	}
	if v.Second < -1 {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "延迟时间不能为空", ""))
	}
	if v.Command == "" {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "command不能为空", ""))
	}
	s = new(models.Queue)
	s.ID = random.RandDateAndString(17)
	s.Name = v.Name
	s.Command = v.Command
	if v.Second == -1 || v.Second == 0 {
		second = 1
	} else {
		second = v.Second
	}

	s.RunAt = time.Now().Add(time.Second * time.Duration(second)).Format("2006-01-02 15:04:05") //TODO 计算出来的时间
	s.CreateAt = time.Now().Format("2006-01-02 15:04:05")
	s.Status = fileds.Unexecuted

	if id, err = controller.repository.InsertQueue(s); err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.QueryError, "报错错误", err))
	}

	return c.JSON(http.StatusOK, responses.Response(200, "成功", id))
}

func (controller *QueueController) GetQueues(c echo.Context) error {
	var (
		jobPlanTable map[string]*processor.JobSchedulePlan
	)

	jobPlanTable = processor.G_scheduler.JobPlanTable
	return c.JSON(http.StatusOK, responses.Response(200, "成功", jobPlanTable))
}

func (controller *QueueController) DeleteJob(c echo.Context) error {
	var (
		v   *requests.QueueRequest
		err error
	)

	v = new(requests.QueueRequest)
	if err = c.Bind(v); err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "绑定参数失败", err))
	}
	if v.Id == "" {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "id不能为空", ""))
	}

	if err = controller.repository.DeleteQueueById(v.Id); err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.QueryError, "删除失败", err))
	}

	return c.JSON(http.StatusOK, responses.Response(200, "成功", ""))

}

func (controller *QueueController) AgainJob(c echo.Context) error {
	var (
		v   *requests.QueueRequest
		err error
	)

	v = new(requests.QueueRequest)
	if err = c.Bind(v); err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "绑定参数失败", err))
	}
	if v.Id == "" {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "id不能为空", ""))
	}

	if err = controller.repository.AgainQueueById(v.Id); err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.QueryError, "再次放入执行器失败,请查看是否执行过", err))
	}

	return c.JSON(http.StatusOK, responses.Response(200, "成功", ""))
}
